﻿import A2.BLK;
import mx.utils.Delegate;
/**
 * ...
 * @author wangjun
 */
class A2.utils.uForm {
	function uForm() {
	}
	//------------------- beautiful line -----------------------
	// [use] 功能函数
	// [usage] _________
	public static function getTargetTF(tf_dele):TextField {
		if (tf_dele.constructor==TextField) {
			return tf_dele;
		}else if(tf_dele.constructor==MovieClip && tf_dele.style == 'mix')
		{ 
			//?????
			//tf_dele.limit = index;
			return tf_dele.tf;
		}else if (tf_dele.constructor==MovieClip) 
		{
			return tf_dele.tf;
		} 
	}
	//------------------- beautiful line -----------------------
	// [use] 格式化
	// [usage] 限制字符，限制字数
	public static function init(index, tf_dele) { 
		
		var tf:TextField = getTargetTF(tf_dele);
		if (index.constructor == String) {
			initStatic(index,tf);
		}else if (index.constructor == Array ) {
			initDefined(index,tf);
		}
	}
	public static function initDefined(index:Array, tf:TextField) {
		var max:Number;
		var res:String;
		
		switch (AS2.Util.getType(index[0])) {
		case 'num' :
			max = index[0];	res = index[1];
			break;
		case 'str' :
			max = index[1];	res = index[0];
			break;
		}
		
		tf.maxChars = max; 
		tf.restrict = res; 
			
	}
	public static function initStatic(index:String,tf:TextField) {
		var max:Number;
		var res:String; 
		
		switch (index.toLowerCase()) {
			case 'username': 	max = 20;	res = '^ ';				
				break;
			case 'mail' : 		max = 6;	res = '[0-9]';			
				break;
			case 'email' : 		max = 50;	res = 'a-zA-Z0-9@.\\-_^ ';	
				break;
			case 'mobile' :
			case 'mobilephone' :max = 11;	res = '[0-9]';			
				break;
			case 'phone' :
			case 'fax' :		max = 20;	res = '[0-9]\\-';		
				break;
			case "passage":		max = 1000;	res = '^ ';		
				/*tf.sharpness
				var tft:TextFormat
				tft.indent*/
				break;
			case "password":
			case "pwd":			max = 15;	res = '***';			
			default :
				break;
		} 
		// 
		
		tf.maxChars = max;
		if (res == '***') {
			tf.password = true; 
		}else{
			tf.restrict = res;
		}
	}
	//------------------- beautiful line -----------------------
	// 检查函数
	// 一般用这个
	public static function check(index:String, content:String) {
		//trace(content)
		switch (index) 
		{
			case 'password':
				return chkPwd(content);
			break;
			case 'email':
				return chkEmailV2(content);
			break;
			case 'mobile':
				return chkMobile(content);
			break; 
			default:
			trace("!! FormV2.Check:: 没有满足第一个参数")
		}
	} 
	static function chkPwd(str:String, str2:String) {
		var str:String = new String(str);
		if (str == '') {
			return ['密码不能为空', 0];
		} else if (str2 == '') {
			return ['密码不能为空', 1];
		} else if (str.length<6) {
			return ['密码至少设置为6位数', 0];
		} else if (str != str2) {
			return ['密码输入不同', 1];
		} else {
			return true;
		}
	}
	static function chkEmailV2(_str:String):Boolean {
		var str:String = new String(_str);
		var arr:Array = str.split("@");
		if (arr.length != 2 || arr[1].indexOf(".")<1) {
			//_root.FFFFmChKKKK = '请确保邮件地址填写正确';
			return (false);
		} else {
			return (true);
		}
	}
	static function chkEmail(_str:String):Boolean {
		var str:String = new String(_str);
		if (str == '' || str.indexOf('@') == -1 || str.indexOf('.') == -1) {
			//_root.FFFFmChKKKK = '请确保邮件地址填写正确';
			return false;
		} else {
			return true;
		}
	}
	static function chkMobile(_str:String):Boolean {
		var str:String = new String(_str);
		if (str.substr(0, 1) != "1") {
			//_root.FFFFmChKKKK = '请填写正确的电话或手机';
			return false;
		}
		if (str.substr(0, 1) == "1") {
			if (str.substr(1, 1) != "3" && str.substr(1, 1) != "5" && str.substr(1, 1) != "8") {
				//_root.FFFFmChKKKK = '请填写正确的电话或手机';
				return false;
			}
		}
		if (str.length != 11) {
			//_root.FFFFmChKKKK = '请确保手机位数11位';
			return false;
		}
		return true;
	}
	//------------------- beautiful line -----------------------
	// 检查字节数 
	public static function checkCharLength(scope:MovieClip,tf:TextField,maxNum:Number,vars:Object):Object {
		var obj:Object = new Object();
		obj.tf = tf;
		obj.maxNum = maxNum;
		obj.onPP = (vars.onPP != null)?vars.onPP:null;
		obj.onPass = (vars.onPass != null)?vars.onPass:null;
		
		 
		tf.onSetFocus = Delegate.create(obj, function() { 
			Delegate.create(scope, obj.onStt)({}); 			
		}) 
		tf.onChanged = Delegate.create(obj, function() { 
			var nowNum:Number = A2.utils.uString.getCharLength(obj.tf.text);
			if (nowNum  > maxNum) { 
				Delegate.create(scope, obj.onPass)({hanzi:-Math.floor((maxNum - nowNum)*.5)}); 
				return;
			}
			Delegate.create(scope, obj.onPP)( { num: obj.tf.text.length,
												dif: maxNum - obj.tf.text.length,
												hanzi:Math.floor((maxNum - nowNum)*.5)
												});
		})
		return obj;
	}
}
